/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-21
 * Time: 18:02
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;

namespace tools
{
	/// <summary>
	/// Base class for all managers. Gives the oportunity to do initialization code,
	/// tear down code, and update code in protected overridden methods. Update won't
	/// work however, until a call to Initialize has been made. After the call to
	/// TearDown it also stops working
	/// 
	/// Implements the Template Method design pattern
	/// </summary>
	public abstract class Manager : IManager
	{
		#region members
		
		/// <summary>
		/// This is to remember if this manager has been initialized or not
		/// </summary>
		private bool		m_WasInitialized;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, intializes members
		/// </summary>
		protected Manager()
		{
			m_WasInitialized = false;
		}
		
		#endregion
		
		#region methods
		
		/// <summary>
		/// Initializes the Manager, once initialized it is not possible to initialize it again
		/// until it has been tore down.
		/// </summary>
		public void Initialize()
		{
			if(!m_WasInitialized)
			{
				DoInitialize();
				m_WasInitialized = true;
			}
		}
		
		/// <summary>
		/// Tears down the Manager, once tore down, it is not possible to tear it down again
		/// until it has been initialized.
		/// </summary>
		public void TearDown()
		{
			if(m_WasInitialized)
			{
				DoTearDown();
				m_WasInitialized = false;
			}
		}
		
		/// <summary>
		/// This should be called once every frame, if the manager is that type of manager
		/// (e.g. EntityManager moving things on the screen) Otherwise, just don't override
		/// method DoUpdate.
		/// </summary>
		/// <param name="deltaTime">The time passed since the last frame</param>
		public void Update(ref int deltaTime)
		{
			if(m_WasInitialized)
			{
				DoUpdate(ref deltaTime);
			}
		}
		
		#endregion
		
		#region virtuals
		
		/// <summary>
		/// This is where the actual initialization should be implemented. Override this method, for
		/// the call to Initialize to actually do some work.
		/// </summary>
		protected virtual void DoInitialize()
		{
		}
		
		/// <summary>
		/// This is where the actual tearing down should be implemented. Override this method, for
		/// the call to TearDown to actually do some work.
		/// </summary>
		protected virtual void DoTearDown()
		{
		}
		
		/// <summary>
		/// This is where the actual updating should be implemented. Override this method, for
		/// the call to Update to actually do some work.
		/// </summary>
		/// <param name="deltaTime">The time passed since the last frame</param>
		protected virtual void DoUpdate(ref int deltaTime)
		{
		}
		
		#endregion
	}
}
